package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.pojo.entity.Cart;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.vo.CartProductVO;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IShippingService;
import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/order")
public class FrontOrderController {

	@Autowired
	private IShippingService shippingService;
	@Autowired
	private ICartService cartService;
	@Autowired
	private IOrderService orderService;

	@RequestMapping("/getOrderConfirmPage.shtml")
	public String getOrderConfirmPage(HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		// 当前用户收货地址信息
		List<Shipping> shippingList = shippingService.selectByUserId(user.getId());
		model.addAttribute("shippingList", shippingList);
		// 当前用户购物车里面"选中"要去结算生成订单的购物车列表
		List<CartProductVO> cartList = cartService.selectByUserIdAndChecked(user.getId());
		model.addAttribute("cartList", cartList);

		return "/front/order_confirm";
	}
	
	@ResponseBody
	@RequestMapping("/insert.shtml")
	public JSONResult insert(Order order,HttpSession session) {
		User user = (User) session.getAttribute("user");
		order.setUserId(user.getId());
		boolean isSuccess = orderService.insert(order);
		return isSuccess ? JSONResult.ok("插入成功"):JSONResult.error("插入失败");
	}
	
	@RequestMapping("/getOrderListPage.shtml")
	public String getOrderListPage() {
		
		return "/front/order_list";
	}
	

}
